package com.kdtech.analyse.Bbs;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.DoMainUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.NumberUtils;
import com.kdtech.utils.StringUtils;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import com.kdtech.crawler.CrawlHTML;

import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.analyse.AnalyseNews;

/**
 * 大众点评  网站解析
 * @author Chase
 *
 */
public class DianpingBbsAnalyse implements AnalyseNews {

	private static final String[] regex = {
		"http://.*.dianping.com/topic/[0-9]*"
	};


	
	public boolean isDetailPage(String url) {
		if(url.indexOf("login/?redir=")!=-1) return false;
		for (int i = 0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta meta = new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String url = urlMeta.getUrl();
		/*
		 * 网站源码
		 */
		String htmltxt = urlMeta.getHtml();

		if(!isDetailPage(url)){
		}


		String title = null;
		String content = null;
		String author = null;
		Long date = null;
		Document doc = Jsoup.parse(htmltxt);

		/*
		 * 标题
		 */
		title = doc.select("title").text();
		title = StringUtils.substringBefore(title, "-");

		/*
		 * 时间
		 */
		date = DateUtils.matchDate(doc.select("span#mainNoteTime").text());

		if(StringUtils.isBlank(title) || date == null){
		}

		/*
		 * 内容
		 */
		content = HtmlCleaner.getContentHtml(url, doc.select("div#mainNoteInfo"));

		/*
		 * 作者
		 */
		if(doc.select("div.user-info > a").size() >= 0){
			author = doc.select("div.user-info > a").get(0).text();
			}

		/*
		 * 链接
		 */
		meta.setUrl(url);

		/*
		 * 来源
		 */
		meta.setTitle(title);
		meta.setContent(content);
		meta.setAuthor(author);
		meta.setDate(date);


		/**
		 * 评论数  点击数
		 */
		Integer clickNum=0;
		Integer commentNum=0;
		String clickNumStr =  null;
		String commentNumStr = null;

		Elements elements = doc.select("div.topic-reply-hits>span");

		if (elements != null && elements.size()>0) {
			String[] split = elements.text().split(" ");
			if(split != null && split.length>0) commentNumStr = split[0];
			if(split != null && split.length>1) clickNumStr = split[1];

			commentNumStr = StringUtils.substringAfter(commentNumStr, "：");
			clickNumStr = StringUtils.substringAfter(clickNumStr, "：");
		}

		try {
			if (StringUtils.isNotBlank(clickNumStr)) clickNum = Integer.valueOf(clickNumStr);
			if (StringUtils.isNotBlank(commentNumStr)) commentNum = Integer.valueOf(commentNumStr);
		} catch (NumberFormatException e) {}

		meta.setClickNum(clickNum);
		meta.setCommentNum(commentNum);
		meta.setUpdateUrl(url);

		return meta;
	}

	

	
	public NewsMeta Update(NewsMeta meta) {
		if(meta!=null){
			String updateUrl = meta.getUpdateUrl();
			if(StringUtils.isNotBlank(updateUrl)){
				UrlMeta urlMeta  = CrawlHTML.responseToURL(updateUrl);
				return parserHtml(urlMeta);
			}
		}
		return null;
	}


	
}
